// pages/infomation/infomation.js
let app = getApp()
import {
  request,
  requestHasToken
} from '../../service/network'

let formatTime = require('../../utils/util.js')
import {
  previewImg,
  previewImgList
} from '../../behavior/previewImg/previewImg.js'
import {
  scrollBackTopBehavior
} from '../../behavior/scrollBackTop/scrollBackTop.js'
Page({
  behaviors: [previewImg, previewImgList, scrollBackTopBehavior], //  继承behavior.js里面的properties，data，methods
  data: {
    baseurl: App.config.baseApi,
    timer: 0,
    inputSeconder: 800,
    page: 0,
    limit: 7,
    createrUserOpenidBoolean: false,
    notLogin: false,
    admin: false,
    normalAdmin: false,
    authority: false,
    infoNumRet: false,
    showModal: false,
    modalMsg: '确定删除吗',
    dataId: '',
    notStateNums: 0,
    genderIndex: 0,
    genders: ['无', '男', '女'],
    ageIndex: 0,
    ages: ['无', '00后', '95后', '90后', '85后', '80后', '75后', '70后', '其他'],
    keywords: '',
    state: '已审核',
    infos: []
  },
  onShareAppMessage: async function (op) {
    // console.log(op);

    let title = '遇见你'
    if (op.target.dataset.type == "transmit") {
      return {
        title,
        path: '/pages/person/person?id=' + op.target.dataset.id,
      }
    } else {
      let authority22 = ''
      if (this.data.authority) {
        await requestHasToken({
          url: 'api/nt/ap/information/getSettings',
          data: {
            type: 'view'
          },
        }).then((res) => {
          if (res.data.code == 0) {
            let godata = res.data.data
            // console.log(godata.valnum);
            //view 1 admin和发帖用户转发可见  2 admin转发可见
            if (godata.valnum == 1) {
              if (this.data.infoNumRet||this.data.admin || this.data.normalAdmin) {
                authority22 = '?authority=true'
              }
            } else if (godata.valnum == 2) {
              if (this.data.admin || this.data.normalAdmin) {
                authority22 = '?authority=true'
              }
            }
          }
        })
      }
      
      return {
        title,
        path: '/pages/infomation/infomation' + authority22,
      }
    }
  },
  onLoad: async function (options) {
    // console.log(app.globalData, options);

    //用户没有授权就去登录页
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          this.notLogin()
        }
      }
    })
    //storage没有userInfo就去登录页
    if (wx.getStorageSync('token') == "") {
      this.notLogin()
    }

    if (app.globalData.authority || options.authority) {
      app.globalData.authority = true
      this.setData({
        authority: true,
      })
    }

    if (app.globalData.admin) {
      this.setData({
        admin: true,
        authority: true,
      })
    }
    if (app.globalData.normalAdmin) {
      this.setData({
        normalAdmin: true,
        authority: true,
      })
    }

    if (!this.data.admin || !this.data.authority) {
      if (!this.data.notLogin) {
        await requestHasToken({
          url: 'api/xcx/auth',
        }).then((res) => {
          if (res.data.code == 0) {
            const user = res.data.user
            const infos = res.data.infos

            if (user.admin == 1) {
              app.globalData.authority = true
              app.globalData.admin = true
              this.setData({
                admin: true,
                authority: true,
              })
              //普通管理员
            } else if (user.admin == 2) {
              app.globalData.normalAdmin = true
              app.globalData.authority = true
              this.setData({
                normalAdmin: true,
                authority: true,
              })
            } else if (infos != null && infos.length > 0) {
              app.globalData.authority = true
              this.setData({
                authority: true,
                infoNumRet: true,
              })
            }
          }
        })
      }
    }
    if (this.data.admin || this.data.normalAdmin) {
      this.notStateNumsMethod()
    }
    if (this.data.authority) {
      this.chooseAddInfos()
    }
  },
  bindscrolltolower(e) {
    this.chooseAddInfos()
    //防抖
    /* if (this.data.authority) {
      clearTimeout(this.data.timer)
      let timer = setTimeout(() =>{
        if (this.data.authority) {
          this.addInfos()
        }
      }, this.data.inputSeconder)
      this.setData({
        timer
      })
    } */
  },
  chooseAddInfos() {
    if (!this.data.notLogin) {
      this.addInfosT()
    } else {
      this.addInfos()
    }
  },
  addInfos() {
    this.setData({
      page: this.data.page + 1
    })
    request({
      url: 'api/nt/ap/information/page',
      data: {
        page: this.data.page,
        limit: this.data.limit,
        type: this.data.state=='未审核'?'':'公开',
        state: this.data.state,
        gender: this.data.genderIndex,
        state: this.data.state,
        keywords: this.data.keywords,
        age: this.data.ages[this.data.ageIndex] == '无' ? '' : this.data.ages[this.data.ageIndex],
      }
    }).then((res) => {
      if (res.data.code == 0) {
        let info2 = this.data.infos
        if (info2 && info2.length > 0) {
          info2.push(...res.data.data)
          this.setData({
            infos: info2,
          })
        } else {
          this.setData({
            infos: res.data.data,
          })
        }
      }
    })
  },
  addInfosT() {
    this.setData({
      page: this.data.page + 1
    })
    requestHasToken({
      url: 'api/ap/information/page',
      data: {
        page: this.data.page,
        limit: this.data.limit,
        type: this.data.state=='未审核'?'':'公开',
        state: this.data.state,
        gender: this.data.genderIndex,
        state: this.data.state,
        keywords: this.data.keywords,
        age: this.data.ages[this.data.ageIndex] == '无' ? '' : this.data.ages[this.data.ageIndex],
      }
    }).then((res) => {
      if (res.data.code == 0) {
        let info2 = this.data.infos
        if (info2 && info2.length > 0) {
          info2.push(...res.data.data)
          this.setData({
            infos: info2,
          })
        } else {
          this.setData({
            infos: res.data.data,
          })
        }
      }
    })
  },
  notLogin() {
    this.setData({
      notLogin: true
    })
  },
  loginPage() {
    if (this.data.notLogin) {
      wx.redirectTo({
        url: '/pages/login/login?url=/pages/infomation/infomation'
      })
      return false
    } else {
      return true
    }
  },
  handleGender(e) {
    if (this.loginPage()) {
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        genderIndex: e.detail.value,
      })
      this.init()
      this.chooseAddInfos()
    }
  },
  handleAge(e) {
    if (this.loginPage()) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        ageIndex: e.detail.value,
      })
      this.init()
      this.chooseAddInfos()
    }
  },
  bindinput(e2) {
    if (this.loginPage()) {
      if (this.data.authority) {
        clearTimeout(this.data.timer)
        //防抖
        let timer = setTimeout(() => {
          this.changeKeywords(e2)
        }, this.data.inputSeconder)
        this.setData({
          timer
        })
      }
    }
  },
  changeKeywords(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      keywords: e.detail.value,
    })
    this.init()
    this.chooseAddInfos()
  },
  radioStateChange(e) {
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
    if (this.data.state == '已审核') {
      this.notStateNumsMethod()
    }
    this.setData({
      state: e.detail.value
    })
    this.init()
    this.chooseAddInfos()
  },
  init() {
    this.setData({
      infos: [],
      page: 0,
    })
  },
  notStateNumsMethod() {
    requestHasToken({
      url: 'api/ap/information/infoStateNum',
      data: {
        state: '未审核',
      }
    }).then((res) => {
      if (res.data.code == 0) {
        let notStateNums = res.data.nums
        this.setData({
          notStateNums
        })
      }
    })
  },
  handleCurd(e) {
    if (this.loginPage()) {
      if (this.data.authority) {
        wx.redirectTo({
          url: '/pages/infoCurd/infoCurd'
        })
      } else {
        wx.redirectTo({
          url: '/pages/addPic/addPic'
        })
      }
    }
  },
  handleInfoItemsDelModal(e) {
    this.setData({
      showModal: true,
      modalMsg: '确定删除' + e.detail.name + '吗?',
      dataId: e.detail.id
    })
  },
  handleInfoItemsDel(e) {
    requestHasToken({
      url: 'api/ap/information/remove',
      data: {
        id: this.data.dataId
      },
    }).then((res) => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '删除成功!',
          icon: 'success',
          duration: 1000
        })
        let b = this.data.infos.filter((item, index, arr) => {
          return item.id != this.data.dataId
        })
        this.setData({
          infos: b
        })
      }
    })
  },
  updateInfosFresh(e) {
    let b = this.data.infos.filter((item, index, arr) => {
      return item.id != e.detail.id
    })
    this.setData({
      infos: b
    })
  },

})